from h3yun.service import goods_service
from utils.clean_utils import is_empty
from config.h3yun_config import pss_site_mate_tableName,pss_goods_mate_tableName
from clean_data.common.common import get_pss_goods_to_h3yun_site_dict, get_pss_site_to_h3yun_site_dict

name_dict = {
    '国药控股广西有限公司': '02023001',
    '国药控股柳州有限公司': '02093002'
}


def mate_CPMC(CPMCBM_PSS, name, exception_msg):
    temp1 = []
    temp2 = []
    temp3 = []
    global name_dict
    goods_code_dict = get_goods_code_dict(name_dict.get(name))
    index = 0
    for item in CPMCBM_PSS:
        goods = get_code(str(item), goods_code_dict, pss_goods_mate_tableName, '产品编码(配送商)', exception_msg, index)
        temp1.append(goods.get('F0000003', ''))  # F0000003 产品名称 (公司的)
        temp2.append(str(item))  # 产品编码（配送商的）
        temp3.append(goods.get('F0000004', ''))  # F0000004 产品编码（公司备案）
        index += 1
    return temp1, temp2, temp3


def mate_YYMC(YYMCBM_PSS, name, exception_msg):
    temp1 = []
    temp2 = []
    temp3 = []
    global name_dict
    yymc_code_dict = get_yymc_code_dict(name_dict.get(name))
    index = 0
    for item in YYMCBM_PSS:
        yymc = get_code(str(item), yymc_code_dict, pss_site_mate_tableName, '客户编码(配送商)', exception_msg, index)
        temp1.append(yymc.get('F0000003', '')) # F0000003 医院名称 （公司的）
        temp2.append(str(item))  # 产品编码（配送商的）
        temp3.append(yymc.get('F0000004', '')) # F0000004 医院编码（公司备案）
        index += 1
    return temp1, temp2, temp3


def get_goods_code_dict(code):
    return get_pss_goods_to_h3yun_site_dict(code)


def get_yymc_code_dict(code):
    return get_pss_site_to_h3yun_site_dict(code)


def get_code(code, code_dict, table_name, column_name, exception_msg, index):
    if is_empty(code_dict.get(code)):
        exception_msg[index] += f'{table_name} 没有{column_name} {code}的产品'
        return {}
    return code_dict.get(code)